package br.ufal.tci.nexos.arcolive.service.broker.command;

import br.ufal.tci.nexos.arcolive.ArCoLIVEServiceManager;
import br.ufal.tci.nexos.arcolive.command.ArCoLIVEClientCommand;
import br.ufal.tci.nexos.arcolive.command.ArCoLIVECommandResult;
import br.ufal.tci.nexos.arcolive.exception.ArCoLIVECannotRemoveParticipantException;
import br.ufal.tci.nexos.arcolive.participant.ArCoLIVEParticipant;
import br.ufal.tci.nexos.arcolive.service.connection.ConnectionRequest;

public class LogoutServiceCommand extends ArCoLIVEClientCommand {
    
	public LogoutServiceCommand(ConnectionRequest request) {
		super(request);
	}
	
	public ArCoLIVECommandResult executeImpl() {
		int serviceId = this.request.getIntegerParameter("serviceId").intValue();
		int instanceId = this.request.getIntegerParameter("instanceId").intValue();
		ArCoLIVEParticipant participant = (ArCoLIVEParticipant) this.request.getOwner();
		String response = "";
		
		try {
			ArCoLIVEServiceManager.getInstance().removeParticipantFromService(serviceId, instanceId, participant);
			response = "1";
		} catch (ArCoLIVECannotRemoveParticipantException e) {
			response = "0";
			return new ArCoLIVECommandResult(this, response, e);
		}
		
		return new ArCoLIVECommandResult(this, response);
	}

}